package me.firstapp.bts.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import me.firstapp.bts.service.SearchJobService;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.ListObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.bbs.JsonTopic;
import me.firstapp.common.module.shop.JsonProduct;
import me.firstapp.spi.bbs.SearchService;
import me.firstapp.spi.bbs.TopicService;
import me.firstapp.spi.shop.ProductService;

@Service
public class SearchJobServiceImpl implements SearchJobService {

	private static Logger logger = LoggerFactory.getLogger(SearchJobService.class);

	@Autowired
	private RedisTemplate<String, Object> redisTemplate;

	@Autowired
	private TopicService topicService;

	@Autowired
	private SearchService searchService;

	@Autowired
	private ProductService productService;

	@Override
	public void updateSearchData() {
		long start = System.currentTimeMillis();
		logger.info("................开始执行搜索服务数据更新任务...............");
		logger.info(".......开始更新话题缓存......");
		@SuppressWarnings("unchecked")
		List<JsonTopic> _topicList = (List<JsonTopic>) redisTemplate.opsForValue().get("topicList");
		if (_topicList != null) {
			logger.info("缓存中有" + _topicList.size() + "条话题数据。");
		}
		ListObject<JsonTopic> topicResult = topicService.findSomeTopics(null, null, 1000000);
		List<JsonTopic> topicList = topicResult.getItems();
		if (topicList != null && !topicList.isEmpty()) {
			int added = topicList.size() - _topicList.size();
			logger.info("话题表里比缓存中多了" + added + "条话题数据");
			redisTemplate.opsForValue().set("topicList", topicList);
			Map<Long, JsonTopic> topicMap = new HashMap<>();
			for (JsonTopic topic : topicList) {
				topicMap.put(topic.getId(), topic);
			}
			redisTemplate.opsForValue().set("topicMap", topicMap);
			logger.info("重新将" + topicList.size() + "条话题数据放入了缓存");
		}
		logger.info(".......结束更新话题缓存......");

		logger.info(".......开始更新商品缓存......");
		@SuppressWarnings("unchecked")
		List<JsonProduct> _productList = (List<JsonProduct>) redisTemplate.opsForValue().get("productList");
		if (_productList != null) {
			logger.info("缓存中有" + _productList.size() + "条商品数据。");
		}
		ListObject<JsonProduct> productResult = productService.findProducts(1);
		List<JsonProduct> productList = productResult.getItems();
		if (productList != null && !productList.isEmpty()) {
			int added = productList.size() - _productList.size();
			logger.info("商品表里比缓存中多了" + added + "条商品数据");
			redisTemplate.opsForValue().set("productList", productList);
			Map<Long, JsonProduct> productMap = new HashMap<>();
			for (JsonProduct product : productList) {
				productMap.put(product.getId(), product);
			}
			redisTemplate.opsForValue().set("productMap", productMap);
			logger.info("重新将" + productList.size() + "条商品数据放入了缓存");
		}
		logger.info(".......结束更新商品缓存......");

		logger.info("开始调用更新搜索服务数据接口......");
		SingleObject<Object> result = searchService.updateData();
		logger.info("结束调用更新搜索服务数据接口......");
		if (StatusHouse.COMMON_STATUS_OK.getCode().equals(result.getCode())) {
			logger.info("更新搜索服务数据成功！");
		} else {
			logger.info("更新搜索服务数据出错！错误信息===》" + result.getMsg());
		}
		logger.info("................结束执行搜索服务数据更新任务...............耗时===》" + (System.currentTimeMillis() - start));
	}

}
